//
//  AppDelegate.m
//  wd-userapp
//
//  Created by Bin Tang on 2022/12/26.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [NSThread sleepForTimeInterval:1];
    CGRect frame = [[UIScreen mainScreen] bounds];
    _window = [[UIWindow alloc] initWithFrame:frame];
    _window.backgroundColor = [UIColor whiteColor];
    [EDSAppDelegateTool sharedInstance].window = _window;
    [[EDSAppDelegateTool sharedInstance] updateRootVC];
    // 设置接受远程控制
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [_window makeKeyAndVisible];
    return YES;
}


/// 程序将要失去Active状态时调用，比如按下Home键或有电话进来
/// 暂停正在执行的任务；
/// 禁止计时器；
/// 减少OpenGL ES帧率；
/// 若为游戏应暂停游戏

/// 对应的applicationWillEnterForeground（将进入前台）
/// @param application application description
- (void)applicationWillResignActive:(UIApplication *)application
{
    debugLog(@"3. 程序暂停");
}

/// 从前台进入后台调用
 
/// 这个方法用来：
/// 释放共享资源；
/// 保存用户数据（写到硬盘）；
/// 作废计时器；
/// 保存足够的程序状态以便下次恢复。
 

/// 对应applicationDidBecomeActive（已经变成前台）
/// @param application application description
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    debugLog(@"4. 程序进入后台");
//    [application setApplicationIconBadgeNumber:0];
}

///  程序即将进去前台时调用，对应applicationWillResignActive（将进入后台）。这个方法用来撤销applicationWillResignActive中做的改变。
/// @param application application description
- (void)applicationWillEnterForeground:(UIApplication *)application
{// 从后台进入前台
    debugLog(@"程序进入前台");
//    [application setApplicationIconBadgeNumber:0];
}

/// 重新启动应用程序处于非活动状态时暂停(或尚未启动)的任何任务。如果应用程序以前在后台，可以选择刷新用户界面。
/// 当程序复原时，另一个名为 applicationDidBecomeActive 委托方法会被调用，在此你可以通过之前挂起前保存的数据来恢复你的应用程序
/// @param application application description
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    debugLog(@"2.程序激活/重新激活");
}

/// 在应用程序即将终止时调用。适当时保存数据。
/// @param application application description
- (void)applicationWillTerminate:(UIApplication *)application
{
    debugLog(@"程序意外暂停");
    
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    debugLog(@"程序内存告警，可能要终止程序！");
    [[SDImageCache sharedImageCache] clearMemory];
//    [[SDImageCache sharedImageCache] setValue:nil forKey:@"dd"];
//    [[EDSCacheManager shareCache] clearCache];
}

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if (self.isForceLandscape)
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else if (self.isForcePortrait)
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    return UIInterfaceOrientationMaskPortrait;
}

#pragma mark - 推送
/*
#ifdef __IPHONE_8_0
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    debugLog(@"ios8以上推送注册成功");
    [application registerForRemoteNotifications];
}
#endif
 */

/*
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    debugLog(@"注册成功");
    if (![deviceToken isKindOfClass:[NSData class]])
        return;
    const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
    NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                          ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                          ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                          ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    debugLog(@"token=%@", hexToken);
}*/

/// NSData转成16进制字符串
/// @param data data description
- (NSString *)getHexStringForData:(NSData *)data
{
    NSUInteger len = [data length];
    debugLog(@"len=%zd", len);
    char *chars = (char *)[data bytes];
    NSMutableString *hexString = [[NSMutableString alloc] init];
    for (NSUInteger i=0; i<len; i++)
    {
        [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];
    }
    return hexString;
}

// 注册失败
/*
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    
}
 */

/**
 收到通知消息的回调，通常此消息意味着有新数据可以读取（iOS 7.0+）
 
 @param application  UIApplication 实例
 @param userInfo 推送时指定的参数
 @param completionHandler 完成回调
 */
/*- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    completionHandler(UIBackgroundFetchResultNewData);
}
*/

#pragma mark - openurl
//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
//{
//    return [[EDSAppDelegateTool sharedInstance] application:application handleOpenURL:url];
//}
//
//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
//{
//    return [[EDSAppDelegateTool sharedInstance] application:application handleOpenURL:url];
//}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    return [[EDSAppDelegateTool sharedInstance] application:app openURL:url options:options];
}

// universalLink
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler
{
    return [[EDSAppDelegateTool sharedInstance] application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
    return YES;
}






@end
